package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.reflect.TypeToken;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.GsonManager;
import com.sgb.kjwl.databinding.FragmentRecommendForYouBinding;
import com.sgb.kjwl.model.entity.enterprise.CompanyListEntity2;
import com.sgb.kjwl.model.entity.enterprise.CopyrightListEntity2;
import com.sgb.kjwl.model.entity.enterprise.IntellpropEntity;
import com.sgb.kjwl.model.entity.enterprise.ListBean;
import com.sgb.kjwl.model.entity.enterprise.PatentEntity;
import com.sgb.kjwl.model.entity.enterprise.PropertyEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.view.ui.activity.enterprise.MakeOverActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.MakeOverDetailActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.NeedActivity;
//import com.sgb.kjwl.view.ui.activity.enterprise.NeedDetailsActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.NewNeedDetailsActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.CompanyAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.IntellpropAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.IpCopyrightAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.NeedAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.PatentAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.PropertyAdapter;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.List;

public class RecommendForYouFragment extends AppFragment {

    FragmentRecommendForYouBinding mBinding;
    private String mType;
    private List mListRecommend;
    private String mCategory;//分类
    private String mNeedType;
    private IntellpropAdapter markAdapter;
    private PatentAdapter patentAdapter;
    private IpCopyrightAdapter copyrightAdapter;
    private CompanyAdapter companyAdapter;
    private PropertyAdapter propertyAdapter;
    private NeedAdapter needAdapter;


    public static RecommendForYouFragment newInstance(String param1, String needType, String category, List list) {
        RecommendForYouFragment fragment = new RecommendForYouFragment();
        Bundle args = new Bundle();
        args.putString("makeOverType", param1);
        args.putString("needType", needType);
        args.putString("category", category);
        args.putSerializable("list", (Serializable) list);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_recommend_for_you, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mType = getArguments().getString("makeOverType");
        mCategory = getArguments().getString("category");
        mNeedType = getArguments().getString("needType");
        initAdapter();
        mBinding.tvRecommendForMore.setOnClickListener(this);
    }

    /**
     * 初始化适配器
     */
    private void initAdapter() {
        mBinding.rvRecommendForYou.setLayoutManager(new LinearLayoutManager(getContext()));
        mListRecommend = (List) getArguments().getSerializable("list");
        String listStr = GsonManager.getInstance().getGson().toJson(mListRecommend);
        switch (mType) {
            case "商标":
                List<IntellpropEntity> strListMark = GsonManager.getInstance().getGson().fromJson(listStr, new TypeToken<List<IntellpropEntity>>() {
                }.getType());
                markAdapter = new IntellpropAdapter(getContext(), strListMark);
                mBinding.rvRecommendForYou.setAdapter(markAdapter);
                markAdapter.setOnItemClick(new AdapterOnItemClick<IntellpropEntity>() {
                    @Override
                    public void onItemClick(IntellpropEntity intellpropEntity, int p) {
                        ActivityJump.jumpToActivity(getActivity(), MakeOverDetailActivity.class, Pair.create("detail", "商标"), Pair.create("id", intellpropEntity.getId()));
                    }
                });
                break;
            case "版权":
                List<CopyrightListEntity2> strListCopy = GsonManager.getInstance().getGson().fromJson(listStr, new TypeToken<List<CopyrightListEntity2>>() {
                }.getType());
                copyrightAdapter = new IpCopyrightAdapter(getContext(), strListCopy);
                mBinding.rvRecommendForYou.setAdapter(copyrightAdapter);
                copyrightAdapter.setOnItemClick(new AdapterOnItemClick<CopyrightListEntity2>() {
                    @Override
                    public void onItemClick(CopyrightListEntity2 copyrightListEntity2, int p) {
                        ActivityJump.jumpToActivity(getActivity(), MakeOverDetailActivity.class, Pair.create("detail", "版权"), Pair.create("id", copyrightListEntity2.getId()));
                    }
                });
                break;
            case "专利":
                List<PatentEntity> strListPatent = GsonManager.getInstance().getGson().fromJson(listStr, new TypeToken<List<PatentEntity>>() {
                }.getType());
                patentAdapter = new PatentAdapter(getContext(), strListPatent);
                mBinding.rvRecommendForYou.setAdapter(patentAdapter);
                patentAdapter.setOnItemClick(new AdapterOnItemClick<PatentEntity>() {
                    @Override
                    public void onItemClick(PatentEntity patentEntity, int p) {
                        ActivityJump.jumpToActivity(getActivity(), MakeOverDetailActivity.class, Pair.create("detail", "专利"), Pair.create("id", patentEntity.getId()));
                    }
                });
                break;
            case "公司":
                List<CompanyListEntity2> strListCompany = GsonManager.getInstance().getGson().fromJson(listStr, new TypeToken<List<CompanyListEntity2>>() {
                }.getType());
                companyAdapter = new CompanyAdapter(getContext(), strListCompany, false);
                mBinding.rvRecommendForYou.setAdapter(companyAdapter);
                break;
            case "资产":
                List<PropertyEntity> strListProperty = GsonManager.getInstance().getGson().fromJson(listStr, new TypeToken<List<PropertyEntity>>() {
                }.getType());
                propertyAdapter = new PropertyAdapter(getContext(), strListProperty);
                mBinding.rvRecommendForYou.setAdapter(propertyAdapter);
                break;
            case "需求":
                List<ListBean> strList = GsonManager.getInstance().getGson().fromJson(listStr, new TypeToken<List<ListBean>>() {
                }.getType());
                needAdapter = new NeedAdapter(getContext(), strList, false);
                mBinding.rvRecommendForYou.setAdapter(needAdapter);
                needAdapter.setOnItemClick(new AdapterOnItemClick<ListBean>() {
                    @Override
                    public void onItemClick(ListBean listBean, int position) {
                        NewNeedDetailsActivity.startactivity(getActivity(), listBean.id + "");
                    }
                });
                break;
        }
//        mBinding.tvRecommendForMore.setVisibility(mListRecommend.size() > 6 ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_recommend_for_more://查看更多
                switch (mType) {
                    case "商标":
                        ActivityJump.jumpToActivity(getContext(), MakeOverActivity.class, Pair.create("position", 0), Pair.create("category", mCategory));
                        break;
                    case "版权":
                        ActivityJump.jumpToActivity(getContext(), MakeOverActivity.class, Pair.create("position", 1), Pair.create("category", mCategory));
                        break;
                    case "专利":
                        ActivityJump.jumpToActivity(getContext(), MakeOverActivity.class, Pair.create("position", 2), Pair.create("category", mCategory));
                        break;
                    case "公司":
                        ActivityJump.jumpToActivity(getContext(), MakeOverActivity.class, Pair.create("position", 3), Pair.create("category", mCategory));
                        break;
                    case "资产":
                        ActivityJump.jumpToActivity(getContext(), MakeOverActivity.class, Pair.create("position", 4), Pair.create("category", mCategory));
                        break;
                    case "需求":
                        switch (mNeedType) {
                            case "1":
                                ActivityJump.jumpToActivity(getContext(), NeedActivity.class, Pair.create("position", 0), Pair.create("category", mCategory));
                                break;
                            case "2":
                                ActivityJump.jumpToActivity(getContext(), NeedActivity.class, Pair.create("position", 1), Pair.create("category", mCategory));
                                break;
                            case "3":
                                ActivityJump.jumpToActivity(getContext(), NeedActivity.class, Pair.create("position", 2), Pair.create("category", mCategory));
                                break;
                            case "4":
                                ActivityJump.jumpToActivity(getContext(), NeedActivity.class, Pair.create("position", 3), Pair.create("category", mCategory));
                                break;
                            case "5":
                                ActivityJump.jumpToActivity(getContext(), NeedActivity.class, Pair.create("position", 4), Pair.create("category", mCategory));
                                break;
                            case "6":
                                ActivityJump.jumpToActivity(getContext(), NeedActivity.class, Pair.create("position", 5), Pair.create("category", mCategory));
                                break;
                        }
                        break;
                }
                break;
        }
    }
}
